The following describes the basic use of variables in Office Basic.
Office Basic の基本的な変数の使用法について説明します。
A variable name can consist of a maximum of 255 characters. The first character of a variable name must be a letter A-Z or a-z. Numbers can also be used in a variable name, but punctuation symbols and special characters are not permitted, with exception of the underscore character ("_"). In Office Basic variable identifiers are not case-sensitive. Variable names may contain spaces but must be enclosed in square brackets if they do.
変数名には、最大 255 文字を使用できます。変数名の最初の文字には、A-Z ないし a-z のいずれかを使用する必要があります。変数名には数字も使用できますが、アンダースコア (「_」) 以外の句読記号や特殊記号は使用できません。Office Basic の変数名では、大文字と小文字は区別されません。変数名にはスペース記号も使用できますが、その場合は大かっこで囲む必要があります。
Examples for variable identifiers:
下記は変数名の例です。
|
MyNumber=5
|
Correct
|
|
MyNumber5=15
|
Correct
|
|
MyNumber_5=20
|
Correct
|
|
My Number=20
|
Not valid, variable with space must be enclosed in square brackets
|
|
[My Number]=12
|
Correct
|
|
DéjàVu=25
|
Not valid, special characters are not allowed
|
|
5MyNumber=12
|
Not valid, variable may not begin with a number
|
|
Number,Mine=12
|
Not valid, punctuation marks are not allowed
|
In Office Basic you don't need to declare variables explicitly. A variable declaration can be performed with the Dim statement. You can declare more than one variable at a time by separating the names with a comma. To define the variable type, use either a type-declaration sign after the name, or the appropriate key word.
Office Basic では、必ずしも変数を明示的に宣言する必要はありません。変数の宣言は、Dim ステートメントで行います。変数名をコンマで区切ることで、1 度に複数の変数を宣言できます。変数型を指定するには、変数名に続けて、型宣言子ないしは該当するキーワードを付けます。
Examples for variable declarations:
下記は型宣言の例です。
|
DIM a$
|
Declares the variable "a" as a String
|
|
DIM a As String
|
Declares the variable "a" as a String
|
|
DIM a$, b As Integer
|
Declares one variable as a String and one as an Integer
|
|
DIM c As Boolean
|
Declares c as a Boolean variable that can be TRUE or FALSE
|
It is very important when declaring variables that you use the type-declaration character each time, even if it was used in the declaration instead of a keyword. Thus the following statements are invalid:
型宣言を行う際には、キーワードではなく型宣言子を用いる場合でも、個々の変数名ごとに型宣言子を付ける必要があるので、この点には特に注意してください。たとえば、下記のような型宣言は無効です。
|
DIM a$
|
Declares "a" as a String
|
|
a="TestString"
|
Type-declaration missing: "a$="
|
![]() | Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type!
特定の変数型として宣言した変数名は、別の型で再宣言することはできません。 |
To force declaration of variables, use the following command:
強制的に変数宣言をするには、下記のコマンドを使用します。
OPTION EXPLICIT
OPTION EXPLICIT
The Option Explicit statement has to be the first line in the module, before the first SUB. Generally, only arrays need to be declared explicitly. All other variables are declared according to the type-declaration character, or - if omitted - as the default type Single.
Option Explicit ステートメントは、モジュールの 1 行目に置き、最初の SUB の前に記述する必要があります。通常こうした明示的な宣言をする必要があるのは、配列だけです。その他の変数は、型宣言子を使って型宣言をすればよく、型宣言が省略された場合はデフォルトで 単精度型 とされます。
Office Basic supports four variable classes:
Office Basic のサポートする変数には 4 つのクラスがあります。+
Numeric variables can contain number values. Some variables are used to store large or small numbers, and others are used for floating-point or fractional numbers.
数値 変数には、数値を収めることができます。これらの変数では大小様々な値を扱うほか、浮動小数点や分数を扱う場合もあります。
String variables contain character strings.
文字列 変数には、一続きの文字を収めることができます。
Boolean variables contain either the TRUE or the FALSE value.
ブール型 変数には、TRUE (真) および FALSE (偽) という2つの値の一方を収めることができます。
Object variables can store objects of various types, like tables and documents within a document.
オブジェクト 変数には、ドキュメントで使用するテーブルやドキュメントなど、各種のオブジェクトを収めることができます。
Integer variables range from -32768 to 32767. If you assign a floating-point value to an integer variable, the decimal places are rounded to the next integer. Integer variables are rapidly calculated in procedures and are suitable for counter variables in loops. An integer variable only requires two bytes of memory. "%" is the type-declaration character.
整数変数には -32768 から 32767 までの整数を収めることができます。こうした整数変数に浮動小数点型の数値を代入すると、小数点以下を丸めた整数値が収められます。整数変数は、プロシージャー内で高速計算が可能であり、またループカウンタ用の変数としても適しています。整数変数が消費するメモリ量は、2 バイトです。型宣言用の記号は「%」です。
Dim Variable%
Dim 変数名%
Dim Variable As Integer
Dim 変数名 As Integer
Long integer variables range from -2147483648 to 2147483647. If you assign a floating-point value to a long integer variable, the decimal places are rounded to the next integer. Long integer variables are rapidly calculated in procedures and are suitable for counter variables in loops for large values. A long integer variable requires four bytes of memory. "&" is the type-declaration character.
ロング整数変数には -2147483648 から 2147483647 までの整数を収めることができます。このロング整数変数に浮動小数点型の数値を代入すると、小数点以下を丸めた整数値が収められます。ロング整数変数は、プロシージャー内で高速計算が可能であり、またループカウンタ用の変数としても適しています。ロング整数変数が消費するメモリ量は4バイトです。型宣言用の記号は「&」です。
Dim Variable&
Dim 変数名&
Dim Variable as Long
Dim 変数名 as Long
Decimal variables can take positive or negative numbers or zero. Accuracy is up to 29 digits.
10 進数の変数は、正の数、負の数、または 0 を受け取ることができます。 桁数は 29 桁までです。
You can use plus (+) or minus (-) signs as prefixes for decimal numbers (with or without spaces).
10 進数には、プラス (+) またはマイナス (-) 記号をプレフィックスとして使用できます (スペースあり、またはスペースなし)。
If a decimal number is assigned to an integer variable, Office Basic rounds the figure up or down.
整数変数に10 進数が代入される場合、Office Basic は数字を切り上げまたは切り捨てます。
Single variables can take positive or negative values ranging from 3.402823 x 10E38 to 1.401298 x 10E-45. Single variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. A Single variable requires 4 bytes of memory. The type-declaration character is "!".
単精度変数は 3.402823 × 10E38 から 1.401298 × 10E-45 までの正または負の値を取ります。単精度変数は浮動小数点方式で数値を扱うため、整数部の桁数が大きくなるほど小数部の精度が小さくなります。 単精度変数の用途としては、それほど高い精度を必要としない数学的な計算処理に適しています。 この場合の計算処理は、整数変数の場合よりも長くかかりますが、倍精度変数よりは短時間で済みます。 単精度変数が消費するメモリ量は 4 バイトです。 型宣言用の記号は「!」です。
Dim Variable!
Dim 変数名!
Dim Variable as Single
Dim 変数名 as Single
Double variables can take positive or negative values ranging from 1.79769313486232 x 10E308 to 4.94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations. Calculations require more time than for Single variables. A Double variable requires 8 bytes of memory. The type-declaration character is "#".
倍精度変数には 1.79769313486232 × 10E308 から 4.94065645841247 × 10E-324 までの正および負の数値を収めることができます。倍精度変数は浮動小数点方式で数値を扱うため、整数部の桁数が大きくなるほど小数部の精度が小さくなります。 倍精度変数の用途としては、高い精度を必要とする計算処理に適しています。 この場合の計算処理は、単精度変数の場合よりも長くかかります。 倍精度変数が消費するメモリ量は 8 バイトです。 型宣言用の記号は「#」です。
Dim Variable#
Dim 変数名#
Dim Variable As Double
Dim 変数名 As Double
Currency variables are internally stored as 64-bit numbers (8 Bytes) and displayed as a fixed-decimal number with 15 non-decimal and 4 decimal places. The values range from -922337203685477.5808 to +922337203685477.5807. Currency variables are used to calculate currency values with a high precision. The type-declaration character is "@".
通貨変数は、内部的には 64 ビット (8 バイト) の数値として格納され、整数部 15 桁と小数部 4 桁の固定小数点方式として表示されます。 通貨変数は -922337203685477.5808 から +922337203685477.5807 までの値を取ります。通貨変数は、高い精度を必要とする金額計算に使用します。 型宣言用の記号は「@」です。
Dim Variable@
Dim 変数名@
Dim Variable As Currency
Dim 変数名 As Currency
String variables can hold character strings with up to 65,535 characters. Each character is stored as the corresponding Unicode value. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 64 Kbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".
文字列変数には、最大 65,535 文字の文字列を収めることができます。格納後の文字列は、対応する Unicode 値に変換されます。文字列変数の用途としては、プログラム内での文字列処理を行う場合や、最大 64 キロバイトの非印刷文字を一時的に格納する場合などに適しています。文字列変数が消費するメモリ量は、格納する文字列の長さに依存します。型宣言用の記号は「$」です。
Dim Variable$
Dim 変数名$
Dim Variable As String
Dim 変数名 As String
Boolean variables store only one of two values: TRUE or FALSE. A number 0 evaluates to FALSE, every other value evaluates to TRUE.
ブール型変数には、TRUE (真) および FALSE (偽) という 2 つの値の一方のみを収めることができます。数値 0 は FALSE に評価され、その他すべての値は TRUE に評価されます。
Dim Variable As Boolean
Dim 変数名 As Boolean
Date variables can only contain dates and time values stored in an internal format. Values assigned to Date variables with Dateserial, Datevalue, Timeserial or Timevalue are automatically converted to the internal format. Date-variables are converted to normal numbers by using the Day, Month, Year or the Hour, Minute, Second function. The internal format enables a comparison of date/time values by calculating the difference between two numbers. These variables can only be declared with the key word Date.
日付変数には、日付と時刻を示す値を内部形式で収めることができます。 日付変数へ値を代入する際に、Dateserial、Datevalue、Timeserial、Timevalue を使用すると、自動的に内部形式へ変換されます。 日付変数の値を通常の数値に変換するには、Day、Month、Year の各関数および Hour、Minute、Second の各関数を使用できます。 時刻や日付は、内部形式に変換することにより、時刻間の比較計算など行えます。 日付変数は、キーワード Date でのみ宣言できます。
Dim Variable As Date
Dim 変数名 As Date
As soon as the variable has been declared, it is automatically set to the "Null" value. Note the following conventions:
宣言された変数には、初期値として「NULL」値が自動的に設定されます。変数型ごとに下記の規約が適用されるので、注意してください。
Numeric variables are automatically assigned the value "0" as soon as they are declared.
数値 変数には、宣言後に「0」が自動的に代入されます。
Date variables are assigned the value 0 internally; equivalent to converting the value to "0" with the Day, Month, Year or the Hour, Minute, Second function.
日付変数 には、初期値として「0」が代入されます。これは、Day、Month、Year の各関数および Hour、Minute、Second の各関数を使用して値を「0」に変換した場合と同じです。
String variables are assigned an empty-string ("") when they are declared.
文字列変数 には、宣言後に空の文字列 ("") が代入されます。
Office Basic knows one- or multi-dimensional arrays, defined by a specified variable type. Arrays are suitable for editing lists and tables in programs. Individual elements of an array can be addressed through a numeric index.
Office Basic では 1 次元および多次元配列を使用することができ、変数宣言をする際に変数型を指定します。配列は、プログラム中でリストやテーブルを操作する場合に適しています。配列の各要素の指定は、数値によるインデックスで行います。
Arrays must be declared with the Dim statement. There are several ways to define the index range of an array:
配列の宣言には、Dim ステートメントを使う必要があります。配列の長さ (インデックス) の指定は、何通りかの方法があります。
|
DIM text$(20)
|
21 elements numbered from 0 to 20
|
|
DIM text$(5,4)
|
30 elements (a matrix of 6 x 5 elements)
|
|
DIM text$(5 to 25)
|
21 elements numbered from 5 to 25
|
|
DIM text$(-15 to 5)
|
21 elements (including 0), numbered from -15 to 5
|
The index range can include positive as well as negative numbers.
インデックスの範囲には、正の値だけでなく負の値も使用できます。
Constants have a fixed value. They are only defined once in the program and cannot be redefined later:
定数値には、固定された一定の値を割り当てます。これらはプログラム中で一度だけ定義することができ、その後再定義することはできません。
CONST ConstName=Expression
CONST ConstName=Expression